package com.sm.controller;


import com.sm.service.MyTeamService;
import com.sm.service.MyVipService;
import com.sm.service.UserService;
import com.sm.util.Msg;
import com.sm.util.PageBean;
import com.sm.util.PageResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 我的团队的controller
 *
 * @author 60915
 */
@RequestMapping("/team")
@Controller
public class MyTeamController {

    @Autowired
    private MyTeamService myTeamService;
    @Autowired
    private UserService userService;
    @Autowired
    private MyVipService myVipService;

    /**
     * 计算我的团队各项人数数量
     *
     * @param userId
     * @return
     */
    @RequestMapping("/myTeam")
    @ResponseBody
    public Msg selectRefereesUserSum(Integer userId) {
        if (userId == null) {
            return Msg.fail().add("msg", "数据异常，请重试");
        }
        //我的团队数量列表
        List<Map<String, Object>> lowerLevelNum = myVipService.getMyLowerLevelNum(userId);

        //通过用户id查询用户的推荐的所有人(团队总人数)
        int sum = myTeamService.selectRefereesUserNumber(userId, null, null);
//
//        //通过用户id查询用户的直接推荐的所有人（直接推荐人数）
//        int directNum = myTeamService.selectRefereesUserNumber(userId);
//
//        //查询除了直接推荐的人的其他间接推荐的人。
//        int otherNumber = sum - directNum;

        //查询推荐的人开通各种身份的人数
        Map<String, Object> map = new HashMap<>();

        //此用户最高会员级别（确定我的团队菜单展示项）
        //我推荐的总人数（多级推荐）
        map.put("sum", sum);
        //我直接推荐的人数（一级推荐）
//        map.put("directNum", directNum);
//        map.put("otherNumber", otherNumber);
        map.put("countList", lowerLevelNum);
        return Msg.success().add("data", map);
    }

    /**
     *
     * @param userId
     * @param type      类型 1会员2，vip3超级VIP，4合作园，5联盟园
     * @param search
     * @param pageBean
     * @return
     */
    @ResponseBody
    @RequestMapping("/getTeamList")
    public Msg getTeamList(@RequestParam("userId") Integer userId,
                           @RequestParam("type") Integer type,
                           @RequestParam(value = "search", required = false) String search, PageBean pageBean) {
        if (type == null || type < 1 || type > 7) {
            return Msg.fail().add("msg", "类型错误");
        }
        PageResult pageResult =  userService.selectListByType(userId, type, search, pageBean);
        return Msg.success().add("data",pageResult);
    }


    /**
     * @param userId
     * @param search
     * @param type     1合作联盟园 2区域经理
     * @param pageBean
     * @return
     */
    @ResponseBody
    @RequestMapping("/getTeamApplyList")
    public Msg getTeamApplyList(@RequestParam("userId") Integer userId,
                                @RequestParam("type") Integer type,
                                @RequestParam(value = "search", required = false) String search, PageBean pageBean) {
        if (type == null || (type != 1 && type != 2)) {
            return Msg.fail().add("msg","类型错误");
        }
        PageResult pageResult = userService.selectApplyList(userId, search, type, pageBean);
        return Msg.success().add("data", pageResult);
    }

}
